package com.example.common.core;

import com.example.util.JsonUtil;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.AccessLevel;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class Result<T> {
    private String message;
    private int status;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    private T data;

    public static <T> Result<T> success() {
        return of(ResultCode.SUCCESS);
    }

    public static <T> Result<T> success(T data) {
        return of(ResultCode.SUCCESS, data);
    }

    public static <T> Result<T> fail() {
        return of(ResultCode.FAIL);
    }

    public static <T> Result<T> fail(T data) {
        return of(ResultCode.FAIL, data);
    }

    public static <T> Result<T> error(String message) {
        return of(ResultCode.FAIL, message);
    }

    public static <T> Result<T> error(String message, T data) {
        return of(ResultCode.FAIL, message, data);
    }

    public static <T> Result<T> of(ResultCode resultCode) {
        return of(resultCode.getCode(), resultCode.getMessage());
    }

    public static <T> Result<T> of(ResultCode resultCode, T data) {
        return of(resultCode.getCode(), resultCode.getMessage(), data);
    }

    public static <T> Result<T> of(ResultCode resultCode, String message) {
        return of(resultCode.getCode(), message);
    }

    public static <T> Result<T> of(ResultCode resultCode, String message, T data) {
        return of(resultCode.getCode(), message, data);
    }

    public static <T> Result<T> of(int code, String message) {
        return of(code, message, null);
    }

    public static <T> Result<T> of(int code, String message, T data) {
        Result<T> result = new Result<>();
        result.setStatus(code);
        result.setMessage(message);
        result.setData(data);
        return result;
    }

    @Override
    public String toString() {
        return JsonUtil.toJson(this);
    }
}
